// pages/document/document.js
Page({

  data: { 
     chineselist:[],typelist:[],near_id:'',goods_id:'',id:'',filepath:'',i:'',last:''
  },

  onLoad: function (options) {
    this.setData({
      goods_id:options.goods_id,
      near_id:options.near_id
    })
    this.gettempletelist()

  },
    gettempletelist(){
       wx.request({
         url: 'https://zujieba.com/Api/template/index.html',
         data:{uid:wx.getStorageSync('uid')},
         method:"POST",
         success:(res)=>{
           console.log(res);
           
          let id=res.data.data.sql.id
          let chineselist= res.data.data.sql
          let typelist=res.data.data.type
          this.setData({chineselist,typelist,id})
            
         }
       })
    },
    preview(e){
      this.throttle(()=>{
        wx.showLoading({
          title: '正在打开...',
        })
       console.log(e.currentTarget.dataset.pic);
       
       let url="https://zujieba.com/file_index/"+e.currentTarget.dataset.pic
       
       wx.downloadFile({
         url: url,
         success:(res1)=>{
           console.log(res1);
           wx.hideLoading()
           wx.openDocument({
             filePath: res1.tempFilePath
           })
         }
       })
      },1500)
       
    },
    toggle(e){
      console.log(e);
      this.setData({id:e.currentTarget.dataset.id,i:e.currentTarget.dataset.i})
      wx.request({
        url: 'https://zujieba.com/Api/template/index.html',
        data:{uid:wx.getStorageSync('uid'),type:e.currentTarget.dataset.id},
        method:"POST",
        success:(res)=>{
          if(res.data.success==2){return this.setData({chineselist:[]})}
            
          console.log(res);
         let chineselist= res.data.data.sql
         let typelist=res.data.data.type
         this.setData({chineselist,typelist})
          
        }
      })
      
       
    },
    //fuzhimoban
    getfuzhi(url,name,page,filename,time){
         wx.request({
           url: 'https://zujieba.com/Api/upload/copy_tem.html',
           data:{filename:url},
           method:'POST',
           success:(res)=>{
             console.log(res);
             let url1=res.data.data

             let url2="https://zujieba.com/uploads/"+url1
             console.log(url2);
             
      
      wx.downloadFile({
        url: url2,
        success:(res1)=>{
          console.log(res1);
           wx.hideLoading({
             complete: (res) => {},
           })
          // wx.navigateTo({
          //   url: '/pages/fileprint/print/print?url='+url2+"&name="+url1+'&page='+page+"&filename="+filename+"&time="+time+'&near_id='+this.data.near_id+"&goods_id="+this.data.goods_id+"&radio5=3"+'&param=1'
          // });
          wx.navigateTo({
            url: '/pages/fileprint/print/newprint?url='+url2+"&name="+url1+'&page='+page+"&filename="+filename+"&time="+time+'&near_id='+this.data.near_id+"&goods_id="+this.data.goods_id+"&radio5=3"+'&param=1'
          });
        }})
           }
         })
    },
    navprint(e){

     this.throttle(()=>{
       wx.showLoading({
         title: '正在上传...',
       })
      let url=e.currentTarget.dataset.url
      let name=e.currentTarget.dataset.name
      let page=e.currentTarget.dataset.page
      let filename=e.currentTarget.dataset.title
      let time=e.currentTarget.dataset.time
      console.log(e);
      this.getfuzhi(url,name,page,filename,time)
      
     },2000)
     
    },
    onPullDownRefresh(){
      if(this.data.id==''){
        return this.gettempletelist()
      }
      wx.request({
        url: 'https://zujieba.com/Api/template/index.html',
        data:{uid:wx.getStorageSync('uid'),type:this.data.id},
        method:"POST",
        success:(res)=>{
          wx.stopPullDownRefresh()
          if(res.data.success==2){return this.setData({chineselist:[]})}
            
            
          console.log(res);
         let chineselist= res.data.data.sql
         let typelist=res.data.data.type
         this.setData({chineselist,typelist})
          
        }
      })
      
    },
   
    //节流
    throttle:function(func,wait){
      console.log(func,wait);
      
      let last =this.data.last
      console.log(last);
      
      let current_time= +new Date()
      if(current_time-last>wait){
        console.log(1111);
          console.log(current_time,current_time-last);
          
          func()
          last=+new Date()
          this.setData({
            last
          })
      }
        
         
      
       
    },
  

})